package com.xiashitech.agent.instrumentation.spring.controller.inst;

import com.google.auto.service.AutoService;
import com.xiashitech.agent.rule.usermatch.Register;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Arrays.asList;

@AutoService(InstrumentationModule.class)
public class XSSpringControllerInstrumentationModule extends InstrumentationModule {

    public XSSpringControllerInstrumentationModule() {
        super("spring-mvc-controller");
    }

    @Override
    public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
        return hasClassesNamed("org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod");
    }

    @Override
    public List<TypeInstrumentation> typeInstrumentations() {
        return asList(new XSSpringControllerMethodInstrumentation());
    }

    @Override
    public List<String> getAdditionalHelperClassNames() {
        List<String> helperClassNames = new ArrayList();
        helperClassNames.addAll(Arrays.asList("com.xiashitech.agent.instrumentation.spring.controller.XSSpringControllerSingletons",
                "com.xiashitech.agent.instrumentation.spring.controller.XSControllerRequestAttributesExtractor",
                "com.xiashitech.agent.instrumentation.spring.controller.apicapture.inst.ApiGenerate",
                "com.xiashitech.agent.instrumentation.spring.controller.apicapture.inst.v_3_1.ApiGenerate_V_3_1",
                "com.xiashitech.agent.instrumentation.spring.controller.apicapture.inst.utils.GenerateSpanAttrUtil",
                "com.xiashitech.agent.instrumentation.spring.controller.apicapture.inst.utils.GenerateParamsUtil"));
        helperClassNames.addAll(Register.registUserMatch());
        return helperClassNames;
    }
}
